package com.spider.utils;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class JsoupUtils { 
	
	private List<String> idList;
	private List<String> diffList;
	private List<String> knowList;
	private List<String> answUrlList;
	private List<String> gradList;
	private List<String> versionList;
	private List<String> typeList;
	private List<List<String>> optionImgUrlList;
	private List<String> imgUrlList;
	private List<String> optionList;

	private List<List<String>> contentList;
	private List<String> contList;
	
//	private Map<String,String> contentMap = new HashMap();
	
	private Matcher match;
	private Pattern pattern;
	
	private List<String> processOptionImg(String cont) {
		List<String> imgUrlList = new ArrayList();
		String regex = "[a-zA-z]+://[^\\s]*png";
		pattern = pattern.compile(regex);
		match = pattern.matcher(cont);
		while (match.find()) {
			imgUrlList.add(match.group());
		}
		if(!match.find()){
			imgUrlList.add("");
		}
		return imgUrlList;
	}

	public static void main(String[] args) {
		new JsoupUtils().resolveOptionWithImg(new File("./text"));
	}

	private List<String> optionResoWithImg(Document doc) {
		optionList = new ArrayList();
		Elements data_id = doc.select("div[id*=optionHtmlList] script");
		for (Element element : data_id) {
			optionList.add(element.html());
		}

		List<String> contentList = new ArrayList();

		for (String str : optionList) {
			String s = str.substring("converToArray.push({".length()).trim()
					.substring("id:'optionHtmlList-62241537',".length()).trim().substring("html:\"[".length());
//			System.out.println(s);®
			// .substring("html::[{::optionHtml:::::".length()).trim());
//			 System.out.println(s.replaceAll("\\{.{2}optionHtml.{5}", ""));
			String[] sp = s.replaceAll("\\{.{2}optionHtml.{5}", "").split("\\\\\"},");
			for (String s1 : sp) {
//				String s2 = s1.replaceAll("\\\\\"}]\"", "").replaceAll("}.;", "").trim();
//				imgUrlList = this.processOptionImg(s2);
////				if (imgUrlList.isEmpty()) {
////					optionImgUrlList.add(null);
////				} else {
//				optionImgUrlList.add(imgUrlList);
////				}
//				s2.replaceAll("&lt;img  border=&#39;0&#39; src=&#39;", "")
//						.replaceAll("[a-zA-z]+://[^\\s]*", "<img>")
//						.replaceAll("&lt;i&gt;", "")
//						.replaceAll("&lt;/i&gt;", "");
				contentList.add(s1.replaceAll("}\\);","").replaceAll("\\\\\"}]\"", ""));
			}
		}
		return contentList;
	}

	private List<List> optionReso(Document doc) {
		optionImgUrlList = new ArrayList();
		optionList = new ArrayList();
		Elements data_id = doc.select("div[id*=optionHtmlList] script");
		for (Element element : data_id) {
			optionList.add(element.html());
		}
		List<String> contentList = new ArrayList<String>();
		for (String str : optionList) {
			String s = str.substring("converToArray.push({".length()).trim()
					.substring("id:'optionHtmlList-62241537',".length()).trim().substring("html:\"[".length());
			// .substring("html::[{::optionHtml:::::".length()).trim());
			// System.out.println(s.replaceAll("\\{.{2}optionHtml.{5}", ""));
			String[] sp = s.replaceAll("\\{.{2}optionHtml.{5}", "").split("\\\\\"},");
			for (String s1 : sp) {
				String s2 = s1.replaceAll("\\\\\"}]\"", "").replaceAll("}.;", "").trim();
				imgUrlList = this.processOptionImg(s2);
//				if (imgUrlList.isEmpty()) {
//					optionImgUrlList.add(null);
//				} else {
					optionImgUrlList.add(imgUrlList);
//				}
				contentList.add(s2.replaceAll("&lt;img  border=&#39;0&#39; src=&#39;", "")
								  .replaceAll("[a-zA-z]+://[^\\s]*", "<img>")
								  .replaceAll("&lt;i&gt;", "")
								  .replaceAll("&lt;/i&gt;", "")
								  );
			}
		}
		List<List> resultList = new ArrayList();
		resultList.add(contentList);
		resultList.add(optionImgUrlList);	
		return resultList;
	}

	public List<List> resolveOption(File file) {
		Document doc = null;
		try {
			doc = Jsoup.parse(file, "UTF-8");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return this.optionReso(doc);
	}

	public List<String> resolveOptionWithImg(File file) {
		Document doc = null;
		try {
			doc = Jsoup.parse(file, "UTF-8");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return this.optionResoWithImg(doc);
	}

	public List<String> resolveOption(String htmlContent) {
		String html = htmlContent;
		Document doc = Jsoup.parse(html);
		return this.optionResoWithImg(doc);
	}
	
	private List<String> idReso(Document doc){
		idList = new ArrayList();
		Elements data_id = doc.select("li.question-box");
		for (Element element : data_id) {
			idList.add(element.attr("data-id"));
		}
		return idList;
	}
	public List<String> resolveId(File file){
		Document doc = null;
		try {
			doc = Jsoup.parse(file,"UTF-8");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return this.idReso(doc);
	}
	public List<String> resolveId(String htmlContent){
		String html = htmlContent;
		Document doc = Jsoup.parse(html);
		return this.idReso(doc);
	}
	
	private List<String> diffReso(Document doc){
		diffList = new ArrayList();
		Elements diff = doc.select("div.f-fr b");
		for (int i = 0; i < diff.size(); i++) {
			if(i % 2 == 1){
				diffList.add(diff.get(i).toString().substring("<b>".length(),"<b>0.39".length()));
				continue;
			}
		}
		return diffList;
	}
	public List<String> resolveDiff(File file){
		Document doc = null;
		try {
			doc = Jsoup.parse(file,"UTF-8");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return this.diffReso(doc);
	}
	public List<String> resolveDiff(String htmlContent){
		String html = htmlContent;
		Document doc = Jsoup.parse(html);
		return this.diffReso(doc);
	}
	
	private String knowReso(Document doc){
		knowList = new ArrayList();
		Elements know = doc.select("div.g-sd a[class~=.*sel.*]");
		for (Element element : know) {
			knowList.add(element.text());
		} 
		if(knowList.isEmpty()) {
			return "";
		}else {
			return knowList.get(0);
		}	
	}
	public String resolveKnow(File file){
		Document doc = null;
		try {
			doc = Jsoup.parse(file,"UTF-8");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return this.knowReso(doc);
	}
	public String resolveKnow(String htmlContent){
		String html = htmlContent;
		Document doc = Jsoup.parse(html);
		return this.knowReso(doc);
	}
	
	private List<String> answUrlReso(Document doc){
		answUrlList = new ArrayList();
		Elements answerUrl = doc.select("div.f-fl a.u-btn");
		for (Element element : answerUrl) {
			answUrlList.add(element.attr("href"));
		}
		return answUrlList;
	}
	public List<String> resolveAnswerUrl(File file){
		Document doc = null;
		try {
			doc = Jsoup.parse(file,"UTF-8");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return this.answUrlReso(doc);
	}
	public List<String> resolveAnswerUrl(String htmlContent){
		String html = htmlContent;
		Document doc = Jsoup.parse(html);
		return this.answUrlReso(doc);
	}
	
	private String gradReso(Document doc){
		gradList = new ArrayList();
		Elements cur = doc.select("div#filter-book dd.z-sel a");
		for (Element element : cur) {
			gradList.add(element.text());
		}
		if(gradList.isEmpty()) {
			return "";
		}else {
			return gradList.get(0);
		}	
	}
	public String resolveGrad(File file){
		Document doc = null;
		try {
			doc = Jsoup.parse(file,"UTF-8");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return this.gradReso(doc);
	}
	public String resolveGrad(String htmlContent){
		String html = htmlContent;
		Document doc = Jsoup.parse(html);
		return this.gradReso(doc);
	}
	
	private String versionReso(Document doc){
		versionList = new ArrayList();
		Elements cur = doc.select("div#filter-version dd.z-sel a");
		for (Element element : cur) {
			versionList.add(element.text());
		}
		if(versionList.isEmpty()) {
			return "";
		}else {
			return versionList.get(0);
		}	
	}
	public String resolveVersion(File file){
		Document doc = null;
		try {
			doc = Jsoup.parse(file,"UTF-8");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return this.versionReso(doc);
	}
	public String resolveVersion(String htmlContent){
		String html = htmlContent;
		Document doc = Jsoup.parse(html);
		return this.versionReso(doc);
	}
	
	private String typeReso(Document doc){
		typeList = new ArrayList();
		Elements type = doc.select("div#fitler-tixing dd.z-sel a");
		for (Element element : type) {
			typeList.add(element.text());
		}
		if(typeList.isEmpty()) {
			return "";
		}else {
			return typeList.get(0);
		}	
	}
	public String resolveType(File file){
		Document doc = null;
		try {
			doc = Jsoup.parse(file,"UTF-8");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return this.typeReso(doc);
	}
	public String resolveType(String htmlContent){
		String html = htmlContent;
		Document doc = Jsoup.parse(html);
		return this.typeReso(doc);
	}
	
	private String processTageU(String cont){
		if(cont.matches(".*<u>.*")){
			cont = cont.replaceAll("<u>.*</u>", "____");
		}
		return cont;
	}
	private List<List<String>> processTageImg(List<String> contenList){
		contList = new ArrayList();
		contentList = new ArrayList();
		imgUrlList = new ArrayList();		
		for(int i = 0;i < contenList.size();i++){
			String cont = contenList.get(i);
			String imgUrl = "";
			if(cont.matches(".*<img .*>.*")){
				pattern = pattern.compile("http.+png");
				match = pattern.matcher(cont);
				if(match.find()){
					imgUrl = match.group();
					cont = cont.replaceAll("<img.*>", "<img>");
					imgUrlList.add(imgUrl);
					contList.add(cont);
				}else{
					pattern = pattern.compile("http.+gif");
					match = pattern.matcher(cont);
					if(match.find()){
						imgUrl = match.group();
						cont = cont.replaceAll("<img.*>", "<img>");
						imgUrlList.add(imgUrl);
						contList.add(cont);
					}else{
						imgUrlList.add("");
						contList.add(cont);
					}
				}
			}else{
				imgUrlList.add("");
				contList.add(cont);
			}
		}
		contentList.add(contList);
		contentList.add(imgUrlList);
		return contentList;
	}
	
	private List<List<String>> contentReso(Document doc){
		List<String> contenList = new ArrayList();
		Elements content = doc.select("div.u-bodyHtml");
		for (Element element : content) {
			String cont = "";
			try {
				cont = element.text().substring("<span class></span>".length());
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
//			cont = ProcessUtils.processSubForJsoup(ProcessUtils.processSupForJsoup(cont));
//			cont = this.processTageU(cont);
			contenList.add(cont);
		}
//		contentList = this.processTageImg(contenList);
		return contentList;
	}

	private List<String> contentResoWithImg(Document doc){
		List<String> contenList = new ArrayList();
		Elements content = doc.select("div.u-bodyHtml");
		for (Element element : content) {
			String cont = "";
			try {
				cont = element.text().substring("<span class></span>".length());
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
//			cont = ProcessUtils.processSubForJsoup(ProcessUtils.processSupForJsoup(cont));
//			cont = this.processTageU(cont);
			contenList.add(cont);
		}
//		contentList = this.processTageImg(contenList);
		return contenList;
	}

	public List<List<String>> resolveContent(File file){
		Document doc = null;
		try {
			doc = Jsoup.parse(file,"UTF-8");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return this.contentReso(doc);
	}
	public List<List<String>> resolveContent(String htmlContent){
		String html = htmlContent;
		Document doc = Jsoup.parse(html);
		return this.contentReso(doc);
	}

	public List<String> resolveContentWithImg(String htmlContent){
		String html = htmlContent;
		Document doc = Jsoup.parse(html);
		return this.contentResoWithImg(doc);
	}
	
//	public static void main(String[] args) throws IOException {
//		File file = new File("./index");
//		JsoupUtils jsoup = new JsoupUtils();
//		Map<String,String> list = jsoup.resolveContent(file);
//		for (String key : list.keySet()) {
//			System.out.println("content:"+key);
//			System.out.println("imgUrl:"+list.get(key));
//		}
//	}
}
